{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# coding:utf-8\n",
    "import jieba\n",
    "from gensim.models.word2vec import Word2Vec\n",
    "from gensim.models import word2vec\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import multiprocessing\n",
    "import gensim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "senteces = word2vec.Text8Corpus('./wiki.seg.txt')\n",
    "model = Word2Vec(senteces,min_count=5,size = 300,window=6,workers=multiprocessing.cpu_count())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# model = gensim.models.Word2Vec.load('wiki.zh.text.model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/fwj/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated `most_similar` (Method will be removed in 4.0.0, use self.wv.most_similar() instead).\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('银行卡', 0.7082972526550293),\n",
       " ('淘宝网', 0.7036200761795044),\n",
       " ('微信', 0.6793803572654724),\n",
       " ('skype', 0.6747853755950928),\n",
       " ('paypal', 0.6726701259613037),\n",
       " ('银联', 0.6684303879737854),\n",
       " ('中国银联', 0.6634664535522461),\n",
       " ('借记卡', 0.6551664471626282),\n",
       " ('转账', 0.6490375399589539),\n",
       " ('服务商', 0.6444780230522156)]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.most_similar('支付宝')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "outp1 = 'wiki.zh.text.model'\n",
    "model.save(outp1)\n",
    "model.wv.save_word2vec_format('char_vector_300.txt', binary=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/fwj/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated `most_similar` (Method will be removed in 4.0.0, use self.wv.most_similar() instead).\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('南', 0.8022689819335938),\n",
       " ('东', 0.5986915230751038),\n",
       " ('西', 0.5708750486373901),\n",
       " ('南至', 0.5564389824867249),\n",
       " ('由北', 0.5431172251701355),\n",
       " ('南到', 0.5247851610183716),\n",
       " ('南抵', 0.5245392322540283),\n",
       " ('今南', 0.5217975974082947),\n",
       " ('东至', 0.5200044512748718),\n",
       " ('北通', 0.5133792757987976)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.most_similar('北')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/fwj/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([-1.66405392, -2.59726429, -5.02488804,  0.02004719,  1.47941267,\n",
       "        0.15073268,  1.3995713 ,  1.65990651,  1.16628945, -0.13080253,\n",
       "        3.77411103, -1.82110262, -1.80528939, -0.82015908, -3.24736619,\n",
       "       -0.24188304, -4.75083637, -0.99472237, -1.65620852,  0.99225295,\n",
       "        4.5373044 , -0.42740643,  0.13584492,  1.53890717, -5.01627636,\n",
       "        0.65130448,  1.63447988, -3.96371269, -0.2432909 , -1.42763352,\n",
       "        1.35989285,  2.93364167, -1.18466401,  0.45444661,  2.85407805,\n",
       "       -0.49093524,  4.22810173,  1.43683493, -0.17669113,  0.69716543,\n",
       "        0.32051599,  2.46782088,  2.87121725,  1.18042707, -0.43865702,\n",
       "        2.1767025 , -3.23615956, -1.03966641,  0.56690103,  4.0696826 ,\n",
       "        2.80142164, -1.60352087,  0.30149648, -1.18771493,  2.30223989,\n",
       "        2.30866504,  4.52355957,  0.60650504,  6.02097368, -0.72810048,\n",
       "        1.42462122,  0.62100613, -2.60997081,  2.07158947,  5.4527998 ,\n",
       "        3.58111715, -0.46009573,  0.20708717,  3.94204736,  3.06454682,\n",
       "        0.283797  , -0.37941381,  1.81906915,  1.43775249, -3.70991564,\n",
       "        3.48310804,  3.65929127,  0.69084579, -2.23873448, -1.26287043,\n",
       "        1.38598859,  0.56687635,  0.91526079, -3.20655322,  1.82699847,\n",
       "        4.88082838,  0.16809057,  2.11139631, -1.76863039,  1.91571796,\n",
       "        0.36378986, -1.54317069, -2.31151605, -1.38281178, -1.81669688,\n",
       "        2.60671544, -3.4243865 ,  0.09266004,  2.13280153, -2.47321939,\n",
       "       -5.25113583,  0.99008858, -1.02818131, -2.09621572, -0.24624398,\n",
       "       -0.04797002,  2.60468411, -2.81472111, -3.7072587 , -3.46639347,\n",
       "        1.24300063,  3.50536346, -1.16390097, -1.5585829 , -6.84169292,\n",
       "       -0.8480534 ,  0.18465222, -0.7919873 ,  1.03755319,  2.22385859,\n",
       "       -2.02594543, -0.84442937, -1.11808479, -1.26929247, -4.12030983,\n",
       "       -2.80821013, -2.61907387,  1.8939091 ,  1.93839717,  0.9229297 ,\n",
       "        1.94697058, -1.26869631, -1.16791332, -0.07336038, -3.0805645 ,\n",
       "       -0.05534911,  3.63976932, -1.18195462,  3.60650659, -1.64499974,\n",
       "       -1.52988946,  0.27911341,  2.34167647,  0.17222469,  1.26859725,\n",
       "        0.29880348,  0.36659035, -0.40993786, -3.65604687,  0.95591545,\n",
       "        6.91671944, -0.44809189,  2.65213013, -4.35645676,  2.96391821,\n",
       "       -3.5651989 , -1.49074686,  1.02017522,  1.73866522,  1.08770144,\n",
       "        0.44119897, -4.65838766, -0.48820484,  0.93166304, -1.45964897,\n",
       "        0.56451398, -3.05495548, -1.83534026,  1.55839014,  8.95056915,\n",
       "       -3.98380995, -0.3871164 , -0.52634197, -0.92856419, -0.02693161,\n",
       "        2.37086964, -2.13550115,  1.95658636,  4.1632514 , -1.81221402,\n",
       "        0.48429132,  3.3015635 , -3.44593787,  3.41716242, -2.8363452 ,\n",
       "        0.76938272, -1.6826098 ,  2.35560942, -0.75752133, -1.65386498,\n",
       "        3.77222824, -1.27550614, -1.24002111, -4.25767231,  5.09388113,\n",
       "        3.3086679 , -1.02327847, -0.13711956, -1.76404703,  1.07834554,\n",
       "       -2.29441714,  0.61405641,  4.44809437, -1.35965633, -2.34686923,\n",
       "       -2.76247549,  4.67856026,  1.22929955, -0.36264575, -1.37737954,\n",
       "        1.45522761,  2.51208377, -2.61459565, -0.38436413, -5.12719297,\n",
       "        2.90839076,  0.28096876, -2.48745275, -1.09476376,  3.03940368,\n",
       "        0.13224305, -2.44057536, -0.44588971,  0.93285888, -3.65967083,\n",
       "        0.0839562 , -1.89911306, -1.08765054,  3.19956851,  0.86070228,\n",
       "       -5.21900415, -3.23255777, -2.0148952 ,  4.90682697,  5.293859  ,\n",
       "       -2.25639153, -0.34012052,  3.29670405,  1.05884504, -0.19790754,\n",
       "        2.34217238, -0.65346873,  0.93197858, -1.01607168, -1.09794688,\n",
       "       -0.60310507,  1.81136417,  1.30593598, -2.10081196, -4.24129486,\n",
       "       -5.81563139, -4.55287933,  0.0682218 ,  1.66802776,  3.36702609,\n",
       "        1.25479734, -4.40566206,  1.06566072, -4.94271469,  1.7888118 ,\n",
       "       -1.22461271,  0.42069358,  1.5337404 , -1.47732496,  1.39760435,\n",
       "        1.54738843,  0.21181253,  0.047237  ,  3.08849406,  3.2784965 ,\n",
       "       -0.49786934,  2.39608932,  0.19571921,  3.21776414,  1.23890841,\n",
       "       -0.29708546,  1.57278645, -1.90344381,  1.34253836,  3.06438637,\n",
       "       -4.49516201,  1.47366512, -6.25425291,  4.24676228,  0.7693733 ,\n",
       "        2.48219943,  1.60409164, -0.01286607,  0.023529  ,  0.02727962,\n",
       "       -5.19357729,  3.34719276,  2.55609608, -0.87319386,  1.52703738,\n",
       "        0.60389674, -1.78837788, -1.72715366,  0.66047424,  2.64796209], dtype=float32)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model['北']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "senteces.max_sentence_length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "senteces = word2vec.Text8Corpus('./wiki.seg.txt')\n",
    "model = Word2Vec(senteces,min_count=500,size =50 ,window=6,workers=multiprocessing.cpu_count())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "outp2 = 'wiki.zh.text_2.model'\n",
    "model.save(outp2)\n",
    "model.wv.save_word2vec_format('char_vector_50.txt', binary=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/fwj/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated `most_similar` (Method will be removed in 4.0.0, use self.wv.most_similar() instead).\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('银行卡', 0.8942134976387024),\n",
       " ('淘宝网', 0.8727918267250061),\n",
       " ('中国银联', 0.8520063757896423),\n",
       " ('网店', 0.8300750255584717),\n",
       " ('银通', 0.8280547857284546),\n",
       " ('个人用户', 0.8266710042953491),\n",
       " ('网上银行', 0.8194146156311035),\n",
       " ('银联', 0.8175311088562012),\n",
       " ('服务商', 0.8123422265052795),\n",
       " ('携程', 0.8119961023330688)]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.most_similar('支付宝')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
